﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class DetailCategory
    {
        #region Properties
        public Language Language
        {
            get
            {
                return this.GetLanguage();
            }
        }

        public Category Category
        {
            get
            {
                return this.GetCategory();
            }
        }
        #endregion

        #region  Methods
        public Language GetLanguage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from languages in _db.Languages
                    where languages.ID == this.ID_Language
                    select languages;
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Category GetCategory()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from categories in _db.Categories
                    where categories.ID == this.ID_Category
                    select categories;
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        #endregion

        #region Static Methods
        public static DetailCategory Parse(System.Object value)
        {
            DetailCategory temp = null;
            try
            {
                temp = (DetailCategory)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static List<DetailCategory> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailCategories;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<DetailCategory> GetList(int id_language)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailCategories.Where(p => p.ID_Language == id_language);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(DetailCategory detailCategory)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (detailCategory.ID_Language < 1)
                errorList.Add(new ErrorItem("ID_Language", "Language is required."));
            if (detailCategory.Description.Length > 2000)
                errorList.Add(new ErrorItem("Description", "Description is only allow up to 2000 characters."));
            if (detailCategory.Name.Length > 100)
                errorList.Add(new ErrorItem("Name", "Name is only allow up to 100 characters."));
            if (detailCategory.Tag.Length > 100)
                errorList.Add(new ErrorItem("Tag", "Tag is only allow up to 100 characters."));
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(DetailCategory ic, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                DetailCategory oi = _db.DetailCategories.Single(p => p.ID_Category == ic.ID_Category && p.ID_Language == ic.ID_Language);
                String content = "";
                content = "[Detail]" + "_" + oi.ID_Language + "_" + oi.Name + "_" + oi.Description + "_" + oi.Tag;
                int errorMess = _db.ChangeDetailCategory(id_User, ic.ID_Category, ic.ID_Language, ic.Name, ic.Description, ic.Tag, content, 0, 0);
                
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(DetailCategory ic, int id_User, int id_Log, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                DetailCategory oi = _db.DetailCategories.Single(p => p.ID_Category == ic.ID_Category && p.ID_Language == ic.ID_Language);
                int errorMess = _db.ChangeDetailCategory(id_User, ic.ID_Category, ic.ID_Language, ic.Name, ic.Description, ic.Tag, "", id_Log, 1);
                
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }
        #endregion

        #region Search
        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID_Category" || kvp.Key == "ID_Language")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "Name" || kvp.Key == "Description" || kvp.Key == "Tag")
                {
                    if (exactList[kvp.Key])
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " = '" + kvp.Value.ToString() + "'";
                    }
                    else
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " LIKE '%" + kvp.Value.ToString() + "%'";
                    }
                }
            }

            return query;
        }

        public static System.Object GetDetailCategory(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<DetailCategory>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }
        #endregion
    }
}